<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class AuthRule_m extends MY_Model
{

	function __construct ()
	{
		parent::__construct();
	}

	public function get_all_groups(){
        $this->db->select('*');
        $this->db->from('auth_rules');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function get_menu_by_auth($auth){
        $sql = "SELECT id,title,pid,name,icon FROM {$this->db->dbprefix}auth_rules  where islink=1 $auth ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function get_current($con,$func){
        $sql = "SELECT s.id,s.title,s.name,s.tips,s.pid,p.pid as ppid,p.title as ptitle FROM {$this->db->dbprefix}auth_rules s left join {$this->db->dbprefix}auth_rules p on p.id=s.pid where s.name='" . $con . '/' . $func . "'";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * 获取启用的规则
     */
    public function get_rules_available($map = "status=1"){
        $sql = "SELECT * FROM {$this->db->dbprefix}auth_rules  where $map ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function delete_menu_by_where($where){
        $this->db->where($where)->delete('auth_rules');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * 查询单条规则
     */
    public function get_rules_by_id($id){
        $query = $this->db
            ->select('*')
            ->get_where('auth_rules', array('id'=>$id));
        return $query->row_array();
    }

    public function sava_menu_rule($uid,$data){

        $this->db->where('id',$uid);
        $this->db->update('auth_rules', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function add_menu_rule($data){

        $this->db->insert('auth_rules',$data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

}
